package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.GenusFeatures;
import interfaces.IGenusFeatures;

public class CGenusFeatures implements IGenusFeatures {
  private CConnection db;

  public CGenusFeatures(CConnection db) {
	this.db = db;
  }
	
  @Override
  public boolean create(GenusFeatures gf) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("INSERT INTO Species_Features VALUES (" + gf.getFeatureID() + ", '" + gf.getGenusName() + "')");
    return i > 0;
  }

  @Override
  public boolean update(GenusFeatures gf) throws SQLException, ClassNotFoundException {
    return false;
  }
  
  @Override
  public boolean delete(GenusFeatures gf) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("DELETE FROM Species_Features WHERE FeatureID = " + gf.getFeatureID() + " AND GenusName = '" + gf.getGenusName() + "'");
    return i > 0;
  }

  @Override
  public ArrayList<GenusFeatures> retrieveAll() throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    ResultSet res = s.executeQuery("select * from genus_features");
		
    ArrayList<GenusFeatures> comb = new ArrayList<GenusFeatures>();
    
    while(res.next()) {
      GenusFeatures sf = new GenusFeatures();
      sf.setFeatureID(res.getInt("FeatureID"));
      sf.setGenusName(res.getString("GenusName"));
      comb.add(sf);
    }
    
    return comb;
  }
}
